﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL_MechSys;
using DTO_MechSys;

namespace FORMS_MechSys
{
    public partial class frmTarefaCadastro : Form
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmTarefaCadastro));

        DataTable dataTableGridView = new DataTable();
        List<DTOTarefa> ListTarefas = new List<DTOTarefa>();

        
        
        DTOTarefa dtoTarefaADD = new DTOTarefa();
        BLLTarefa bllTarefa = new BLLTarefa();

        DTOCor dtoCorPrincipal = new DTOCor();
        DTOCor dtoCorFundo = new DTOCor();
        DTOCor dtoCorLetra = new DTOCor();
        DTOCor dtoCorDataGrid = new DTOCor();

        BLLCor bllCor = new BLLCor();

        public frmTarefaCadastro()
        {
            InitializeComponent();
            PopulaGrid();
            btn_barraUp_Click(null, null);
            Habilitar(false);
            SetColors();
        }

        private void SetColors()
        {
            dtoCorPrincipal = bllCor.Buscar("Principal");

            btnCancelar.BackColor = toolStrip1.BackColor = tlp_dados.BackColor = label4.BackColor = btnNovo.BackColor =
                btn_barraDown.BackColor = btn_barraUp.BackColor = tableLayoutPanel4.BackColor = btnSalvar.BackColor =
                btn_pesquisar.BackColor = btnEditar.BackColor = btnExcluir.BackColor = tableLayoutPanel6.BackColor = dtoCorPrincipal.Cor;

            dtoCorFundo = bllCor.Buscar("Fundo");
            tableLayoutPanel1.BackColor = dtoCorFundo.Cor;

            dtoCorDataGrid = bllCor.Buscar("DataGrid");
            dtGridProdutos.BackgroundColor = dtoCorDataGrid.Cor;

            dtoCorLetra = bllCor.Buscar("Letra");
            label8.ForeColor = label9.ForeColor = toolStripButtonSair.ForeColor =
            label4.ForeColor = label6.ForeColor = btnCancelar.ForeColor = btn_pesquisar.ForeColor =
            btnExcluir.ForeColor = btnNovo.ForeColor = btnSalvar.ForeColor = btnCancelar.ForeColor =
            btnEditar.ForeColor = dtoCorLetra.Cor;


        }


        //EFEITOS
        #region
        private void btnNovo_EnabledChanged(object sender, EventArgs e)
        {
            if (btnNovo.Enabled)
            {
                btnNovo.BackColor = dtoCorPrincipal.Cor;
                btnNovo.ForeColor = Color.White;
                this.btnNovo.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));
                btnNovo.BackColor = dtoCorPrincipal.Cor;
            }
            else
                btnNovo.BackColor = dtoCorFundo.Cor;

        }

        private void btnEditar_EnabledChanged(object sender, EventArgs e)
        {
            if (btnEditar.Enabled)
            {
                btnEditar.BackColor = dtoCorPrincipal.Cor;
                btnEditar.BackColor = dtoCorPrincipal.Cor;
                btnEditar.ForeColor = Color.White;
                this.btnEditar.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));

            }
            else
                btnEditar.BackColor = dtoCorFundo.Cor;
        }

        private void btnCancelar_EnabledChanged(object sender, EventArgs e)
        {
            if (btnCancelar.Enabled)
            {
                btnCancelar.BackColor = dtoCorPrincipal.Cor;

                btnCancelar.BackColor = dtoCorPrincipal.Cor;
                btnCancelar.ForeColor = Color.White;
                this.btnCancelar.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));

            }
            else
                btnCancelar.BackColor = dtoCorFundo.Cor;
        }

        private void btnExcluir_EnabledChanged(object sender, EventArgs e)
        {
            if (btnExcluir.Enabled)
            {

                btnExcluir.BackColor = dtoCorPrincipal.Cor;
                btnExcluir.ForeColor = Color.White;
                this.btnExcluir.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));

            }
            else
                btnExcluir.BackColor = dtoCorFundo.Cor;
        }

        private void btnSalvar_EnabledChanged(object sender, EventArgs e)
        {
            if (btnSalvar.Enabled)
            {
                btnSalvar.BackColor = dtoCorPrincipal.Cor;
                btnSalvar.ForeColor = Color.White;
                this.btnSalvar.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));
            }
            else
                btnSalvar.BackColor = dtoCorFundo.Cor;
        }

        private void btnEditar_MouseLeave(object sender, EventArgs e)
        {
            if (btnEditar.Enabled)
            {
                btnEditar.BackColor = dtoCorPrincipal.Cor;
                btnEditar.ForeColor = Color.White;
                this.btnEditar.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));
            }
        }

        private void btnEditarkkkkkkk(object sender, EventArgs e)
        {
            btnEditar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(194)))), ((int)(((byte)(224)))), ((int)(((byte)(255)))));
            btnEditar.ForeColor = Color.Black;
            this.btnEditar.BackgroundImage = ((System.Drawing.Image)(null));
        }

        private void btnCancelar_MouseLeave(object sender, EventArgs e)
        {
            if (btnCancelar.Enabled)
            {
                btnCancelar.BackColor = dtoCorPrincipal.Cor;
                btnCancelar.ForeColor = Color.White;
                this.btnCancelar.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));
            }
        }

        private void btnCancelarkkkkkkk(object sender, EventArgs e)
        {
            btnCancelar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(194)))), ((int)(((byte)(224)))), ((int)(((byte)(255)))));
            btnCancelar.ForeColor = Color.Black;
            this.btnCancelar.BackgroundImage = ((System.Drawing.Image)(null));
        }

        private void btnExcluir_MouseLeave(object sender, EventArgs e)
        {
            if (btnExcluir.Enabled)
            {
                btnExcluir.BackColor = dtoCorPrincipal.Cor;
                btnExcluir.ForeColor = Color.White;
                this.btnExcluir.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));
            }
        }

        private void btnExcluirkkkkkkk(object sender, EventArgs e)
        {
            btnExcluir.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(194)))), ((int)(((byte)(224)))), ((int)(((byte)(255)))));
            btnExcluir.ForeColor = Color.Black;
            this.btnExcluir.BackgroundImage = ((System.Drawing.Image)(null));

        }

        private void btnSalvar_MouseLeave(object sender, EventArgs e)
        {
            if (btnSalvar.Enabled)
            {
                btnSalvar.BackColor = dtoCorPrincipal.Cor;
                btnSalvar.ForeColor = Color.White;
                this.btnSalvar.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));
            }
        }

        private void btnSalvarkkkkkkk(object sender, EventArgs e)
        {
            btnSalvar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(194)))), ((int)(((byte)(224)))), ((int)(((byte)(255)))));
            btnSalvar.ForeColor = Color.Black;
            this.btnSalvar.BackgroundImage = ((System.Drawing.Image)(null));
        }

        private void btnNovo_MouseLeave(object sender, EventArgs e)
        {
            if (btnNovo.Enabled)
            {
                btnNovo.BackColor = dtoCorPrincipal.Cor;
                btnNovo.ForeColor = Color.White;
                this.btnNovo.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnEditar.BackgroundImage")));
            }
        }

        private void btnNovokkkkkkk(object sender, EventArgs e)
        {
            btnNovo.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(194)))), ((int)(((byte)(224)))), ((int)(((byte)(255)))));
            btnNovo.ForeColor = Color.Black;
            this.btnNovo.BackgroundImage = ((System.Drawing.Image)(null));
        }

        #endregion

        public void Limpar()
        {
            txtCodigo.Clear();
            txtDescricao.Clear();
            txtPesquisar.Clear();
        }

        public void Habilitar(bool status)
        {
            tlp_dados.Enabled = status;
            btnNovo.Enabled = !status;
            btnEditar.Enabled = !status;
            btnExcluir.Enabled = !status;
            btnCancelar.Enabled = status;
            btnSalvar.Enabled = status;

            if (dtGridProdutos.Rows.Count <= 0)
            {
                btnEditar.Enabled = false;
                btnExcluir.Enabled = false;
            }
        }

        private void btnNovo_Click(object sender, EventArgs e)
        {
            Habilitar(true);
            Limpar();
            dtoTarefaADD.TarefaID = 0;

            txtDescricao.Focus();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Habilitar(false);
            Limpar();
        }

        public void PopulaGrid()
        {
            DataRow dataRow;
            dataTableGridView = new DataTable();
            ListTarefas = bllTarefa.BuscarTarefasCadastrados();
            if (ListTarefas.Count > 0)
            {
                dataTableGridView.Columns.Add("TarefaID", typeof(int));
                dataTableGridView.Columns.Add("Descrição", typeof(string));

                foreach (DTOTarefa dtoTarefa in ListTarefas)
                {
                    dataRow = dataTableGridView.NewRow();

                    dataRow["TarefaID"] = dtoTarefa.TarefaID;
                    dataRow["Descrição"] = dtoTarefa.Descricao;

                    // Adiciona linha ao DataTable.
                    dataTableGridView.Rows.Add(dataRow);
                }
            }
            dtGridProdutos.DataSource = null;
            dtGridProdutos.DataSource = dataTableGridView;


        }

        public void PopulaGrid(string descricao, int codigo)
        {
            DataRow dataRow;
            dataTableGridView = new DataTable();

            ListTarefas = bllTarefa.PesquisarTarefa(descricao, codigo);
            if (ListTarefas.Count > 0)
            {
                dataTableGridView.Columns.Add("TarefaID", typeof(int));
                dataTableGridView.Columns.Add("Descrição", typeof(string));


                foreach (DTOTarefa dtoTarefa in ListTarefas)
                {
                    dataRow = dataTableGridView.NewRow();

                    dataRow["TarefaID"] = dtoTarefa.TarefaID;
                    dataRow["Descrição"] = dtoTarefa.Descricao;

                    // Adiciona linha ao DataTable.
                    dataTableGridView.Rows.Add(dataRow);
                }
            }
            dtGridProdutos.DataSource = null;
            dtGridProdutos.DataSource = dataTableGridView;

        }

        private void btnSalvar_Click(object sender, EventArgs e)
        {
            dtoTarefaADD.Descricao = txtDescricao.Text;
            if (!string.IsNullOrWhiteSpace(txtDescricao.Text))
                dtoTarefaADD.Descricao = (txtDescricao.Text);
             

            if (dtoTarefaADD.TarefaID == 0)
                bllTarefa.Inserir(dtoTarefaADD);
            else
                bllTarefa.Atualizar(dtoTarefaADD);
            Limpar();
            PopulaGrid();
            Habilitar(false);
        }

        private void dtGridProdutos_DataSourceChanged(object sender, EventArgs e)
        {
            if (dtGridProdutos.Rows.Count > 5)
                this.WindowState = FormWindowState.Maximized;

            if (dtGridProdutos.Rows.Count <= 0)
            {
                btnEditar.Enabled = false;
                btnExcluir.Enabled = false;
            }
        }

        private void btnExcluir_Click(object sender, EventArgs e)
        {
            if (new frmMessageBox().Show("Tem certeza que deseja apagar esta tarefa permanentemente?", "Aviso", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                dtGridProdutos_CellClick(null, null);
                bllTarefa.Excluir(dtoTarefaADD.TarefaID);
                Limpar();
                Habilitar(false);
                PopulaGrid();
                dtGridProdutos_CellClick(null, null);
            }
        }

      
        private void btnEditar_Click(object sender, EventArgs e)
        {
            dtGridProdutos_CellClick(null, null);
            Habilitar(true);
            txtDescricao.Focus();
        }

        private void dtGridProdutos_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtGridProdutos.CurrentCellAddress.X != -1 && dtGridProdutos.CurrentCellAddress.Y != -1)
            {
                dtoTarefaADD.TarefaID = Int32.Parse(dtGridProdutos.Rows[dtGridProdutos.CurrentCellAddress.Y].Cells["TarefaID"].Value.ToString());
                txtCodigo.Text = dtoTarefaADD.TarefaID.ToString();
                txtDescricao.Text = dtGridProdutos.Rows[dtGridProdutos.CurrentCellAddress.Y].Cells["Descrição"].Value.ToString();
                

                Habilitar(false);
                btnEditar.Enabled = true;
            }
        }

        private void toolStripButtonSair_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_barraDown_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            this.tableLayoutPanel1.RowStyles[0] = (new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 37F));
            this.tableLayoutPanel1.RowStyles[1] = (new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 100F));
            this.tableLayoutPanel1.RowStyles[2] = (new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50F));
            this.tableLayoutPanel1.RowStyles[3] = (new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            btn_barraDown.Visible = false;
        }

        private void btn_barraUp_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            this.tableLayoutPanel1.RowStyles[0] = (new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 0F));
            this.tableLayoutPanel1.RowStyles[1] = (new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 100F));
            this.tableLayoutPanel1.RowStyles[2] = (new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50F));
            this.tableLayoutPanel1.RowStyles[3] = (new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            btn_barraDown.Visible = true;

        }

        private void btn_pesquisar_Click(object sender, EventArgs e)
        {
            int cod = new int();

            Int32.TryParse(txtPesquisar.Text, out cod);
            PopulaGrid(txtPesquisar.Text + "%", cod);

        }


    }
}
